// pages/market/market.js
var app = getApp()
const request = require('../../utils/request');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true, //是否显示面板指示点
    autoplay: true, //是否自动切换
    interval: 3000, //自动切换时间间隔
    duration: 1000, //滑动动画时长
    show: false,
    see: false,
    shopList: [],
    page: 1,
    count: 10,
    typeList: [],
    bannerList: [],
    searchKey: ""
  },
  click: function() {
    var that = this
    this.data.see = !this.data.see;
    this.setData({
      see: this.data.see
    });
  },
  onClickNav({
    detail = {}
  }) {
    this.setData({
      mainActiveIndex: detail.index || 0
    });
  },

  onClickItem({
    detail = {}
  }) {
    this.setData({
      activeId: detail.id
    });
  },
  onClose() {
    var that = this
    this.data.show = false;
    this.setData({
      show: this.data.show
    });
  },
  ShowSelect: function() {
    var that = this;
    this.data.show = true;
    this.setData({
      show: this.data.show
    })
  },
  swichNav: function(e) {
    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  // 轮播图
  getBanner() {
    wx.showLoading()
    request.get('banner_list')
      .then(res => {
        wx.hideLoading()
        this.getType();
        if (res.data && res.data.length) {
          res.data.forEach(v => {
            const regex = /\\/g
            const url = v.banner_img
            if (regex.test(url)) {
              v.banner_img = url.replace(/\\/g, '/')
            }
          })

          this.setData({
            bannerList: res.data
          })
        }
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },
  // 分类
  getType() {
    wx.showLoading()
    const data = {
      pid: 0,
    }
    request.get('goods_type', data)
      .then(res => {
        wx.hideLoading()
        this.getShoplist();
        this.setData({
          typeList: res.data
        })
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },
  toDetail: function(e) {
    wx.navigateTo({
      url: '../market/marketDetail/marketDetail?goods_id=' + e.currentTarget.dataset.id
    })
  },
  toTypeList: function(e) {
    wx.navigateTo({
      url: '../market/xitong/xitong?type_id=' + e.currentTarget.dataset.id + '&type_name=' + e.currentTarget.dataset.name,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getBanner();
  },
  getShoplist() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: app.d.severUrl + 'all_goods',
      method: 'GET',
      data: {
        page: that.data.page,
        key: that.data.searchKey
      },
      success: res => {
        wx.hideLoading();
        if (res.data.code == 0) {
          if (that.data.page == 1 && res.data.data.length == 0){
            wx.showToast({
              icon: 'none',
              title: "暂无商品"
            })
            
          }
          var content = that.data.shopList.concat(res.data.data);
          content.forEach(item => {
            item.cover = app.d.imgPath + item.cover

          })
          that.setData({
            shopList: content,
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg
          })
        }
      },
      fail: error => {
        wx.showToast({
          icon: 'none',
          title: "网络错误，请稍后重试"
        })
      }
    })
  },
  // 搜索
  searchText(e) {
    this.setData({
      searchKey: e.detail.value,
    })
  },
  toSearch() {
    this.setData({
      page: 1,
      shopList: []
    })
    this.getShoplist()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (that.data.page * that.data.count > that.data.shopList.length && that.data.shopList.length > 0) {
      wx.showToast({
        title: '已加载全部数据',
      })
    } else if (that.data.shopList.length > 0) {
      that.data.page = that.data.page + 1;
      that.getShoplist();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})